package com.earnings.okhttputils.utils.god.GodAdapter;

import android.view.View;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by Administrator on 2017/5/20 0020.
 */

public class GodArrayList<T> extends ArrayList {
    private GodBaseAdapter adapter;

    public GodArrayList() {

    }

    public GodArrayList(GodBaseAdapter adapter) {
        this.adapter = adapter;
    }

    @Override
    public boolean addAll(int index, Collection c) {
        boolean bos = super.addAll(index, c);
        if (adapter != null) adapter.notifyDataSetChanged();
        return bos;
    }

    public void setAdapter(GodBaseAdapter adapter) {
        this.adapter = adapter;
    }

    public void setHeaderViewLine(boolean HeaderViewLine) {
        adapter.setHeaderViewLine(HeaderViewLine);
    }

    @Override
    public void add(int index, Object element) {
        super.add(index, element);
        if (adapter != null) adapter.notifyDataSetChanged();
    }

    public ArrayList<GodData> getOneChoice() {
        return adapter.getOneChoice();
    }

    //获取到单选被选中的索引
    public int getOptindex() {
        return adapter.getOptindex();
//        if (adapter.getOneChoice() == null || adapter.getOneChoice().size() == 0) return -1;
//        for (int i = 0; i < adapter.getOneChoice().size(); i++) {
//            GodData data = (GodData) adapter.getOneChoice().get(i);
//            if (data.isOpt()) return i;
//        }
//        return -1;
    }


    @Override
    public void clear() {
        super.clear();
        if (adapter != null) adapter.notifyDataSetChanged();
    }

    //设置为全不选状态
    public void setAllNoOpt() {
        for (int i = 0; i < getOneChoice().size(); i++) {
            getOneChoice().get(i).setOpt(false);
        }
        if (adapter != null) adapter.notifyDataSetChanged();
    }

    public void setOneChoice(boolean isOneChoice) {
        adapter.setOneChoice(isOneChoice);
    }

    public void setDefaultposition(int position) {
        adapter.setDefaultposition(position);
    }


    public void setHeaderView(View headerView) {
        adapter.setHeaderView(headerView);
    }

    public View getHeaderView() {
        return adapter.getHeaderView();
    }


    public void notifyDataSetChanged() {
        if (adapter != null) adapter.notifyDataSetChanged();
    }

    public void notifyItemChanged(int position) {
        if (adapter != null) adapter.notifyItemChanged(position);
    }

    public int getItemCount() {
        return adapter.getHeaderView() == null ? size() : size() + 1;
    }

    @Override
    public T get(int index) {
        return (T) super.get(index);
    }

    @Override
    public boolean removeAll(Collection c) {
        boolean bos = super.removeAll(c);
        if (adapter != null) adapter.notifyDataSetChanged();
        return bos;
    }

    @Override
    public boolean remove(Object o) {
        boolean bos = super.remove(o);
        if (adapter != null) adapter.notifyDataSetChanged();
        return bos;
    }

    @Override
    public Object remove(int index) {
        Object bos = super.remove(index);
        if (adapter != null) adapter.notifyDataSetChanged();
        return bos;
    }

    @Override
    public boolean addAll(Collection c) {
        boolean bos = super.addAll(c);
        if (adapter != null) adapter.notifyDataSetChanged();
        return bos;
    }


}
